import { createRouter, createWebHashHistory } from 'vue-router'

const router = createRouter({
  history: createWebHashHistory(),
  routes: [
    {
      path: '/',
      redirect: '/login',
    },
    {
      path: '/login',
      name: 'login',
      component: () => import('@/views/login/index.vue'),
    },
    {
      path: '/processcontrol',
      name: 'processcontrol',
      component: () => import('@/views/processcontrol/index.vue'),
    }
  ]
})


// 路由守卫
/**
 * 全局前置路由守卫，每一次路由跳转前都进入这个 beforeEach 函数
 */
router.beforeEach((to, from, next) => {

  if (to.path == '/login' || to.path == '/' || to.path=='/processControl') {
      // 登录或者注册才可以往下进行
      next();
  } else {
      // 获取 登录信息
      const info = localStorage.getItem('info'); 
      // token 不存在
      if (info === null || Object.keys(info).length === 0) {
          ElMessage.error('您还没有登录，请先登录');
          next('/login');
      } else {
          next();
      }
  }
});

export default router
